package com.glq1218.security.exception;

import com.alibaba.fastjson2.JSON;
import com.glq1218.response.ResponseResult;
import com.glq1218.util.WebUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义认证失败处理
 */
@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
        authException.printStackTrace();
        ResponseResult<?> responseResult = ResponseResult.error(401, authException.getMessage());
        WebUtils.renderString(response, JSON.toJSONString(responseResult));
    }
}
